﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class DocSequenceDAO
    {

        private DocSequenceDTO CreateDocSeqDTOFromEntity(MaSoChungTu ms)
        {
            DocSequenceDTO docSeq = new DocSequenceDTO
            {
                DocSequenceID = ms.MaSoID,
                DocSequenceCode = ms.MaSoCode,
                DocSequenceName = ms.MaSoName,
                DocSequenceText = ms.MaSoText,
                Separator = ms.DauPhanCach,
                CurrentNum = ms.SoHienTai ?? 0,
                Middle = ms.Giua,
                NumLength = ms.DoDaiSo ?? 0,
                DocSequenceTemp = GetNextDocSequenceNumber(ms.MaSoName)
            };

            return docSeq;
        }

        /// <summary>
        /// Get DocSequence By Name of DocSequence
        /// </summary>
        /// <param name="name">Name of DocSequence</param>
        /// <returns>DocSequenceDTO</returns>
        public DocSequenceDTO GetDocSequenceByName(string name)
        {
            MaSoChungTu doc;

            using (BookStoreEntities ent = new BookStoreEntities())
            {
                 doc = ent.MaSoChungTus.FirstOrDefault(m => m.MaSoName == name);
                 if (doc == null)
                 {
                     return null;
                 }
            }

            DocSequenceDTO docSequence = CreateDocSeqDTOFromEntity(doc);

            return docSequence;
        }

        public bool CheckDocSequenceExists(string code)
        {
            int count = 0;

            using (BookStoreEntities ent = new BookStoreEntities())
            {
                ent.MaSoChungTus.Where(m => m.MaSoCode == code).Count();
            }

            if (count > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool UpdateNextDocSequence(string name)
        {
            using(BookStoreEntities ent = new BookStoreEntities())
            {
                MaSoChungTu doc = ent.MaSoChungTus.FirstOrDefault(m => m.MaSoName == name);
                if (doc == null)
                {
                    return false;
                }

                doc.SoHienTai += 1;

                ent.SaveChanges();
                return true;
            }
        }

        public bool UpdateDocSequence(DocSequenceDTO doc)
        {
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                MaSoChungTu msct = ent.MaSoChungTus.FirstOrDefault(m => m.MaSoID == doc.DocSequenceID);
                if (msct == null)
                {
                    return false;
                } 

                msct.MaSoCode = doc.DocSequenceCode;
                msct.DoDaiSo = doc.NumLength;
                msct.Giua = doc.Middle;
                msct.DauPhanCach = doc.Separator;

                ent.SaveChanges();
                return true;
            }
        }

        public string GetNextDocSequenceNumber(string name)
        {
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                MaSoChungTu doc = ent.MaSoChungTus.FirstOrDefault(m => m.MaSoName == name);
                if(doc == null)
                {
                    return string.Empty;
                }

                string nextNum = string.Empty;
                string format = string.Empty;
                int length = int.Parse(doc.DoDaiSo.ToString());

                for (int i = 0; i < length; i++)
                {
                    format += "0";
                }

                int curNum = int.Parse(doc.SoHienTai.ToString());
                nextNum = (curNum + 1).ToString(format);
                string code = string.Empty;

                if (doc.Giua != null && !string.IsNullOrEmpty(doc.Giua))
                {
                    code = string.Format("{0}{1}{2}{3}{4}", doc.MaSoCode, doc.DauPhanCach, doc.Giua, doc.DauPhanCach, nextNum);
                }
                else
                {
                    code = doc.MaSoCode + doc.DauPhanCach + nextNum;
                }

                return code;
            }
        }

        public List<DocSequenceDTO> GetListDocSequence()
        {
            try
            {
                using (BookStoreEntities ent = new BookStoreEntities())
                {
                    List<MaSoChungTu> listMSCT = ent.MaSoChungTus.ToList();
                    List<DocSequenceDTO> listDocSeq = new List<DocSequenceDTO>();

                    foreach (MaSoChungTu msct in listMSCT)
                    {
                        listDocSeq.Add(CreateDocSeqDTOFromEntity(msct));

                    }

                    return listDocSeq;
                }
            }
            catch
            {
                return null;
            }
        }

        public string GetNextDocSequenceNumber(int id)
        {
            using (BookStoreEntities ent = new BookStoreEntities())
            {
                MaSoChungTu doc = ent.MaSoChungTus.FirstOrDefault(m => m.MaSoID == id);
                if (doc == null)
                {
                    return string.Empty;
                }

                string nextNum = string.Empty;
                string format = string.Empty;
                int length = int.Parse(doc.DoDaiSo.ToString());

                for (int i = 0; i < length; i++)
                {
                    format += "0";
                }

                int curNum = int.Parse(doc.SoHienTai.ToString());
                nextNum = (curNum + 1).ToString(format);
                string code = string.Empty;

                if (doc.Giua != null && !string.IsNullOrEmpty(doc.Giua))
                {
                    code = string.Format("{0}{1}{2}{3}{4}", doc.MaSoCode, doc.DauPhanCach, doc.Giua, doc.DauPhanCach, nextNum);
                }
                else
                {
                    code = doc.MaSoCode + doc.DauPhanCach + nextNum;
                }

                return code;
            }
        }
    }
}
